#include "server.hpp"

#define HOST "127.0.0.1"
#define USER "root"
#define PWD "mzx15378261830"
#define DB_NAME "gobang"
#define PORT 3306


void log_test()
{
    LOG(INF, "hello mzx");
    LOG(DEG, "hello dhb");
    LOG(ERR, "hello hp"); 
}

void mysql_util_test()
{
    MYSQL* mysql = mysql_util::mysql_create(HOST, USER, PWD, DB_NAME, PORT);
    if (mysql == nullptr)
        exit(-1);
    if (mysql_util::mysql_execute(mysql, "insert stu values(null, '张三', 18, 53, 68, 87);") == false)
        exit(-1);
    mysql_util::mysql_destroy(mysql);
}

void json_util_test()
{
    Json::Value root1;
    root1["姓名"] = "张三";
    root1["年龄"] = 18;
    root1["成绩"].append(83);
    root1["成绩"].append(81);
    root1["成绩"].append(96);

    std::string s;
    json_util::serialize(root1, s);
    LOG(DEG, "%s", s.c_str());

    Json::Value root2;
    if (!json_util::deserialize(s, root2))
        exit(-1);
    LOG(DEG, "姓名：%s, 年龄：%d", root2["姓名"].asCString(), root2["年龄"].asInt());
    int size = root2["成绩"].size();
    for (int i = 0; i < size; i++)
        LOG(DEG, "成绩：%.2f", root2["成绩"][i].asFloat());
}

void string_util_test()
{
    std::string s = "123,234,,,,345,,,";
    std::vector<std::string> v;
    string_util::split(s, ",", v);
    for (int i = 0; i < v.size(); i++)
    {
        LOG(DEG, "%s", v[i].c_str());
    }
}

void file_util_test()
{
    std::string filename = "./makefile";
    std::string body;
    if (!file_util::read(filename, body))
        exit(-1);
    LOG(DEG, "%s", body.c_str());
}

void db_test()
{
    user_table ut(HOST, USER, PWD, DB_NAME, PORT);
    Json::Value user;
    user["username"] = "fky";
    user["password"] = "123123";
    //ut.insert(user);
    // if (ut.login(user))
    // {
    //     LOG(DEG, "login success");
    // }

    // Json::Value val;
    // ut.select_by_id(2, val);
    // std::string s;
    // json_util::serialize(val, s);
    // LOG(DEG, "%s", s.c_str());

    // if (ut.win(1))
    //     LOG(DEG, "user win");

    if (ut.lose(1))
        LOG(DEG, "user lose");
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_hall(uid, conn);
    if (om.is_in_hall(uid))
    {
        LOG(DEG, "user id:%d in hall", uid);
    }
    else
    {
        LOG(DEG, "user id:%d not in hall", uid);
    }
    om.exit_hall(uid);
    if (om.is_in_hall(uid))
    {
        LOG(DEG, "user id:%d in hall", uid);
    }
    else
    {
        LOG(DEG, "user id:%d not in hall", uid);
    }
}

void room_test()
{
    user_table ut(HOST, USER, PWD, DB_NAME, PORT);
    online_manager om;
    //room r(10, &ut, &om);
    room_manager rm(&ut, &om);
    rm.create_room(1, 2);
    LOG(DEG, "user1 at room%lu", rm.get_room_by_uid(1)->get_room_id());
    LOG(DEG, "user2 at room%lu", rm.get_room_by_uid(2)->get_room_id());
    rm.remove_user_from_room(1);
    rm.remove_user_from_room(2);
}

void matcher_test()
{
    user_table ut(HOST, USER, PWD, DB_NAME, PORT);
    online_manager om;
    room_manager rm(&ut, &om);
    matcher<uint64_t> mc(&ut, &rm, &om);
}

int main()
{
    gobang_server svr(HOST, USER, PWD, DB_NAME, PORT);
    svr.start(8085);
    return 0;
}
